﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using MySql.Data.MySqlClient;

namespace MovieSystem.Banco
{
   public class EntSessao
   {
       private int intSessaoId = 0;

       public int Id
       {
           get { return intSessaoId; }
           set { intSessaoId = value; }
       }
       private int intFilmeSessaoId = 0;

       public int FilmeSessaoId
       {
           get { return intFilmeSessaoId; }
           set { intFilmeSessaoId = value; }
       }
       private int intSalaSessaoId = 0;

       public int SalaSessaoId
       {
           get { return intSalaSessaoId; }
           set { intSalaSessaoId = value; }
       }
       private DateTime dtHorarioSessao;

       public DateTime HorarioSessao
       {
           get { return dtHorarioSessao; }
           set { dtHorarioSessao = value; }
       }
       private DateTime dtInicioSessao;

       public DateTime InicioSessao
       {
           get { return dtInicioSessao; }
           set { dtInicioSessao = value; }
       }
       private DateTime dtFimSessao;

       public DateTime FimSessao
       {
           get { return dtFimSessao; }
           set { dtFimSessao = value; }
       }
       public Boolean Inserir()
       {

           int intNumero = 0;
           if (EntConexao.AbrirConexao())
           {
               MySqlCommand commS = new MySqlCommand("Insert into Sessao " +
                                                    "(idFilmeSessao, idSalaSessao, horarioSessao, dataInicioSesao, dataFimSesao) values " +
                                                    "('" + this.FilmeSessaoId + "', '" + this.SalaSessaoId + "', '" + this.HorarioSessao + "', '" + this.dtInicioSessao + "', '" + this.dtFimSessao + "');", EntConexao.bdConn);
               intNumero = commS.ExecuteNonQuery();

               EntConexao.FecharConexao();

           }


           return true;

       }

       public Boolean Alterar()
       {

           int intNumero = 0;
           if (EntConexao.AbrirConexao())
           {
               MySqlCommand commS = new MySqlCommand("update Sessao set idFilmeSessao= '" + this.FilmeSessaoId + "', idSalaSessao= '" + this.SalaSessaoId + "', horarioSessao = '" + this.HorarioSessao + "'" +
                                                     " dataInicioSesao='" + this.dtInicioSessao + "', dataFimSesao='" + this.dtFimSessao + "'" +
                                                     " where idSessao = " + this.Id + ";", EntConexao.bdConn);
               intNumero = commS.ExecuteNonQuery();

               EntConexao.FecharConexao();

           }


           return true;

       }

       public Boolean Excluir()
       {

           int intNumero = 0;
           if (EntConexao.AbrirConexao())
           {
               MySqlCommand commS = new MySqlCommand("delete from Sessao where idSessao = " + this.Id + ";", EntConexao.bdConn);
               intNumero = commS.ExecuteNonQuery();

               EntConexao.FecharConexao();

           }


           return true;

       }

       public EntSessao BuscaPorId(string parametro)
       {
           EntSessao oSessao = new EntSessao();

           if (EntConexao.AbrirConexao())
           {
               MySqlCommand commS = new MySqlCommand("SELECT * FROM sessao where idSessao =" + parametro + "", EntConexao.bdConn);
               MySqlDataReader reader = commS.ExecuteReader();

               while (reader.Read())
               {
                   oSessao = new EntSessao();
                   oSessao.Id = Convert.ToInt32(reader["idSessao"].ToString());
                   oSessao.FilmeSessaoId = Convert.ToInt32(reader["idFilmeSessao"].ToString());
                   oSessao.SalaSessaoId = Convert.ToInt32(reader["idSalaSessao"].ToString());
                   oSessao.HorarioSessao = Convert.ToDateTime(reader["horarioSessao"].ToString());
                   oSessao.InicioSessao = Convert.ToDateTime(reader["dataInicioSesao"].ToString());
                   oSessao.FimSessao = Convert.ToDateTime(reader["dataFimSesao"].ToString());

               }

               EntConexao.FecharConexao();
           }
           return oSessao;
       }

       public List<EntSessao> BuscaPorParametro(string parametro)
       {
           List<EntSessao> lSessao = new List<EntSessao>();

           if (EntConexao.AbrirConexao())
           {
               MySqlCommand commS = new MySqlCommand("SELECT * FROM sessao where idFilmeSessao ='" + parametro + "' or horarioSessao='" + parametro + "'  ", EntConexao.bdConn);
               MySqlDataReader reader = commS.ExecuteReader();

               while (reader.Read())
               {
                   EntSessao oSessao = new EntSessao();
                   oSessao.Id = Convert.ToInt32(reader["idSessao"].ToString());
                   oSessao.FilmeSessaoId = Convert.ToInt32(reader["idFilmeSessao"].ToString());
                   oSessao.SalaSessaoId = Convert.ToInt32(reader["idSalaSessao"].ToString());
                   oSessao.HorarioSessao = Convert.ToDateTime(reader["horarioSessao"].ToString());
                   oSessao.InicioSessao = Convert.ToDateTime(reader["dataInicioSesao"].ToString());
                   oSessao.FimSessao = Convert.ToDateTime(reader["dataFimSesao"].ToString());
                   lSessao.Add(oSessao);

               }

               EntConexao.FecharConexao();
           }
           return lSessao;
       }

       public List<EntSessao> BuscaTodas()
       {
           List<EntSessao> lSessao = new List<EntSessao>();

           if (EntConexao.AbrirConexao())
           {
               MySqlCommand commS = new MySqlCommand("SELECT * FROM usuario;", EntConexao.bdConn);
               MySqlDataReader reader = commS.ExecuteReader();

               while (reader.Read())
               {
                   EntSessao oSessao = new EntSessao();
                   oSessao.Id = Convert.ToInt32(reader["idSessao"].ToString());
                   oSessao.FilmeSessaoId = Convert.ToInt32(reader["idFilmeSessao"].ToString());
                   oSessao.SalaSessaoId = Convert.ToInt32(reader["idSalaSessao"].ToString());
                   oSessao.HorarioSessao = Convert.ToDateTime(reader["horarioSessao"].ToString());
                   oSessao.InicioSessao = Convert.ToDateTime(reader["dataInicioSesao"].ToString());
                   oSessao.FimSessao = Convert.ToDateTime(reader["dataFimSesao"].ToString());
                   lSessao.Add(oSessao);

               }

               EntConexao.FecharConexao();
           }
           return lSessao;
       }
   }
}
